package com.xmy.dao.Impl;

import com.xmy.dao.BaseDao;
import com.xmy.dao.MasterDao;
import com.xmy.entity.Master;
import com.xmy.entity.Pet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MasterDaoImpl extends BaseDao implements MasterDao {
    @Override
    public List<Master> SelMaster() {
        List<Master> masters = new ArrayList<Master>();
        String sql = "select mid,loginid from master";
        ResultSet rs = executeQuery(sql,null);
        try{
            while (rs.next()){
                Master master = new Master();
                master.setMid(rs.getInt(1));
                master.setLoginid(rs.getString(2));
                masters.add(master);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return masters;
    }

    @Override
    public Master SelLogin(String name, String pwd) {
        String sql = "select loginid,balance,count(1) from master where loginid = ? and pwd = ?";
        Master master = null;
        ResultSet rs = null;
        int count = 0;
        try{
            rs = executeQuery(sql,new Object[]{name,pwd});
            while (rs.next()){
                master = new Master();
                master.setLoginid(rs.getString(1));
                master.setBalance(rs.getInt(2));
                count = rs.getInt(3);
            }
            if (count==0){
                master = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return master;
    }
}
